'use strict';
const http = require("http"), fs = require('fs'), url = require("url"), querystring = require("querystring"),
	PORT = 94;

global.HLOG = require("./module/wloger").init("http");
//if(process.argv[2]) PORT = process.argv[2];

process.on('uncaughtException', function(err) {
  HLOG.error('uncaughtException: ', err.stack);
});

var server = http.createServer(function(req, res){
	//console.log('httpreq', req.method);
	req.GET = url.parse(req.url, true);
	let hconfig = require("./config/http");
	HLOG.info(req.url);
	if(req.GET.pathname in hconfig){
		res.setHeader('Content-Type', 'text/html;charset=UTF-8');
		res.setHeader('Access-Control-Allow-Origin', '*');
		res.setHeader("Cache-Control", "no-store");//res.setHeader("Cache-Control", "no-cache");
		let runobj = require(hconfig[req.GET.pathname])
		if(runobj.run(req, res)) res.end();	
	}else{
		res.statusCode = 404;
		res.end('Not found');
	}
});

server.on('error', function(e){
	HLOG.error("server error ", e);
});

server.listen(PORT, function() { //'listening' listener
	HLOG.error('http server bound');
});